// pages/index/index.js
var app = getApp();
Page({
  data: {
    tabFixed: false, //tab定位
    tab: {
      list: [],
      selectedId: '',
      selectedIndex: 0,
      sliderItems: [],
      rect: null,
      scrollLeft: 0
    },
    windowWidth: '',
  },
  tabchange(e) {
    let self = this
    let dataset = e.currentTarget.dataset
    self.setData({
      [`tab.selectedIndex`]: dataset.index,
      [`tab.selectedId`]: dataset.tabid,
      [`tab.scrollLeft`]: (dataset.index - 2) * 60
    }, () => {
      if (!self.data.tab.list[`${dataset.index}`].update) {
        self.getBook()
      }
    })
  },
  updasign() {
    app.getApi.updasign({

    }).then({

    })
  },
  getBook(beginrow) {
    wx.showLoading({
      title:'loading...'
    })
    let self = this
    let selectedIndex = self.data.tab.selectedIndex
    let tab = self.data.tab
    let list = tab.list[`${selectedIndex}`]
    // if(list.loading)
    beginrow = beginrow || (list[`beginrow`])
    app.getToken(token => {
      setTimeout(() => {
        app.getApi.getclassification({
          beginrow: beginrow,
          pagecount: 12,
          classify_id: list.id,
          token:token
        }).then(res => {
          console.log(res)
          wx.hideLoading()
          if (beginrow == 0) { //初始化
            list.book_lists = res.data
            list.update = true
          } else {
            list.book_lists = list.book_lists.concat(res.data)
          }
          list.islastline = res.islastline
          list.beginrow = beginrow
          list.loading = false
          self.setData({
            tab: tab
          })
        })
      }, 1000)
    })
  },
  getplaypic() {
    let self = this
    app.getToken(token => {
      app.getApi.getplaypic({ token: token }).then(res => {
        console.log(res)
      }).then(() => {
        self.getBook()
      })
    })
  },
  bindchange(e) {
    if (e.detail.source != "touch") {
      return
    }
    let self = this
    let current = e.detail.current
    let id = this.data.tab.list[`${current}`].id
    // let winwidth = this.data.windowWidth
    this.setData({
      [`tab.selectedIndex`]: current,
      [`tab.selectedId`]: id,
      [`tab.scrollLeft`]: (current - 2) * 60
    }, () => {
      if (!self.data.tab.list[`${current}`].update) {
        self.getBook()
      }
    })
  },
  onMyEvent() {
    wx.navigateTo({
      url: '../search/index'
    })
  },
  lower(e) {
    let self = this
    let selectedIndex = self.data.tab.selectedIndex
    let list = self.data.tab.list[`${selectedIndex}`]
    if (list.loading || list.islastline) { //是否到底(0：否，1：是)
      return
    }
    this.setData({
      [`tab.list[${selectedIndex}].loading`]: true
    }, () => {
      let beginrow = self.data.tab.list[`${selectedIndex}`][`beginrow`]
      self.getBook(beginrow+12)
    })
  },
  getbooktype() {
    let self = this
    let tab = self.data.tab
    app.getApi.getbooktype({}).then(res => {
      if (res.code == '3000') {
        res.data.forEach((item, index) => {
          item.title = item.type_name,
            item.book_lists = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            item.beginrow = 0,
            item.islastline = 0,
            item.update = false,
            item.loading = false
        })
        tab.list = res.data
        tab.selectedId = res.data[0].id
        tab.selectedIndex = 0
        tab.rect = 908
        self.setData({
          [`tab`]: tab
        }, () => {
          self.getBook()
        })
      }
    }).cata
  },
  bindViewTap(e) {
    console.log(e)
  },
  goDetail(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../detail/index?id=${id}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    self.setData({ windowWidth: wx.getSystemInfoSync().windowWidth }, () => {
      self.getbooktype()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '天天享读，让图书传递爱',
      imageUrl: '../../assets/images/index_s.png',
      path: '/pages/index/index'
    }
  }
})